package com.mls.fivedistrict.util;

import java.util.Random;

/**
 * Created by chenxiuxiang on 2018/3/22.
 */
public class TransitionUtil {

    public static   TransitionEntity waveTo4300(double[] sourceWave, double[] sourceInvity) {
        TransitionEntity transitionEntity = new TransitionEntity();
        double[] newWave;
        double[] newInternsity;
        int cutLen = 200;
        //算出需要添加的波数个数，并根据计算出的缺少数据个数初始化返回数据大小
        int setLength = 4325 - (int) sourceWave[sourceWave.length - 1];
        if (setLength <= 0) {
            newWave = new double[sourceWave.length];
            newInternsity = new double[sourceWave.length];
            System.arraycopy(sourceWave, 0, newWave, 0, sourceWave.length);
            System.arraycopy(sourceInvity, 0, newInternsity, 0, sourceInvity.length);
            //返回
            transitionEntity.setNewWave(newWave);
            transitionEntity.setNewInstity(newInternsity);
            return transitionEntity;

        }
        newWave = new double[sourceWave.length + setLength];
        newInternsity = new double[sourceWave.length + setLength];
        System.arraycopy(sourceWave, 0, newWave, 0, sourceWave.length);
        System.arraycopy(sourceInvity, 0, newInternsity, 0, sourceInvity.length);
        //取最后200个数据
        double[] lastIntensity = new double[cutLen];
        int lastIndex = 0;
        for (int i = sourceInvity.length-1; i > sourceInvity.length - cutLen; i--) {
            lastIntensity[lastIndex] = sourceInvity[i];
            lastIndex++;
        }
        double avgLastIntensity;
        double minLastIntensity = lastIntensity[0];
        double sum = 0;

        for (int i = 0; i < lastIntensity.length; i++) {
            sum += lastIntensity[i];
            if (lastIntensity[i] < minLastIntensity) {
                minLastIntensity = lastIntensity[i];
            }
        }
        avgLastIntensity = sum / lastIntensity.length;
        int count = 1;
        for (int i = sourceWave.length; i < sourceWave.length + setLength; i++) {
            newWave[i] = sourceWave[sourceWave.length - 1] + count;
            int index = new Random().nextInt((int) (avgLastIntensity * 1.5));
            newInternsity[i] = minLastIntensity + index;
            count++;
        }
        transitionEntity.setNewWave(newWave);
        transitionEntity.setNewInstity(newInternsity);
        return transitionEntity;
    }

    public static void main(String[] args) {
        double[] x = new double[2000];
        double[] y = new double[2000];
        for (int i=0;i<2000;i++) {
            x[i] = i;
        }
        for (int i=0;i<2000;i++) {
            y[i] = i;
        }
        waveTo4300(x, y);
    }
}
